package impl.comp;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import fr.irit.smac.may.lib.components.messaging.receiver.AgentRef;
import interfaces.infr.IInfo;
import interfaces.infr.ISave;
import sudoemerg.Grille;

public class GrilleImpl extends Grille {
	private int[] tab = new int[81];
	private Hashtable<AgentRef,Integer> link = new Hashtable<AgentRef, Integer>();
	private Hashtable<Integer,AgentRef> linkInverse = new Hashtable<Integer,AgentRef>();

	@Override
	protected IInfo info() {
		// TODO Auto-generated method stub
		return new IInfo() {
			public int getNum(AgentRef ref) {
				return tab[link.get(ref)];
			}
			
			public ArrayList<AgentRef> getListeVoisins(AgentRef agRef) {
				ArrayList<AgentRef> res = new ArrayList<AgentRef>();
				int moi = link.get(agRef);
				int module = link.get(agRef) % 9;
				//Ligne
				for(int i=(moi-module);i<(moi-module+9);i++) {
					if(i!=moi && tab[i]==0)
						res.add(linkInverse.get(i));
				}
				//colonne
				for(int i=module;i<81;i+=9) {
					if(!res.contains(linkInverse.get(i)) && i!=moi && tab[i]==0)
						res.add(linkInverse.get(i));
				}
				int zone = chercheDebutZone(moi);
				//zone
				for(int j=0;j<3;j++) {
					for(int i=zone;i<zone+3;i++) {
						if(!res.contains(linkInverse.get(i)) && i!=moi && tab[i]==0)
							res.add(linkInverse.get(i));
					}
					zone+=9;
				}

				return res;
			}

			public void setNum(AgentRef ref, int num) {
				tab[link.get(ref)] = num;
				gui().refresh(link.get(ref), num);
			}
		};
	}

	@Override
	protected ISave save() {
		// TODO Auto-generated method stub
		return new ISave() {
			
			public void saveLink(AgentRef agRef, int i) {
				link.put(agRef,i);
				linkInverse.put(i,agRef);
			}
			
			public void initByTest(int i) {
				for(int t = 0; t<81; tab[t++]=0);
				switch(i) {
					case 1:
						System.out.println("--Grille--> Chargement de la grille de test numero 1");
						tab[2] = 2;
						tab[3] = 8;
						tab[6] = 7;
						tab[7] = 1;
						tab[8] = 6;
						tab[11] = 5;
						tab[12] = 7;
						tab[15] = 2;
						tab[16] = 3;
						tab[20] = 6;
						tab[23] = 2;
						tab[28] = 5;
						tab[30] = 6;
						tab[32] = 8;
						tab[39] = 3;
						tab[43] = 7;
						tab[45] = 1;
						tab[51] = 4;
						tab[53] = 8;
						tab[54] = 2;
						tab[56] = 4;
						tab[58] = 1;
						tab[65] = 3;
						tab[67] = 9;
						tab[68] = 6;
						tab[70] = 4;
						tab[71] = 1;
						tab[79] = 9;
						break;
					case 2:
						tab[0] = 1;
						tab[3] = 7;
						tab[8] = 2;
						tab[9] = 6;
						tab[12] = 4;
						tab[19] = 7;
						tab[21] = 8;
						tab[23] = 2;
						tab[24] = 4;
						tab[25] = 6;
						tab[32] = 4;
						tab[33] = 1;
						tab[36] = 4;
						tab[39] = 5;
						tab[40] = 7;
						tab[41] = 6;
						tab[45] = 2;
						tab[49] = 8;
						tab[51] = 5;
						tab[53] = 3;
						tab[56] = 4;
						tab[57] = 9;
						tab[64] = 2;
						tab[65] = 6;
						tab[72] = 7;
						tab[79] = 9;
						tab[80] = 1;
						break;
					default:
						System.out.println("!! --Grille--> Grille de test inconnue");
				}
				gui().initAffichage(tab);
			}
		};
	}
	
	private int chercheDebutZone(int i) {
		int divise = i/9;
		if(divise<3)
			divise=0;
		else if(divise<6)
			divise = 3*9;
		else
			divise = 6*9;
		int module = i%9;
		if(module<3)
			return divise;
		else if(module<6)
			return divise+3;
		return divise+6;
	}
}
